//
//  GoodsMoreViewController.m
//  wedding
//
//  Created by 常宽 on 15/1/29.
//  Copyright (c) 2015年 常宽. All rights reserved.
//

#import "GoodsMoreViewController.h"
#import "TypeModel.h"               ///类型的model类
#import "GoodsMoreCollectionViewCell.h" /// 自定义集合cell
#import "ProGoodsViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface GoodsMoreViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>
/// 类型数据数组
@property (nonatomic, retain) NSMutableArray *typeArray;
/// 集合视图
@property (nonatomic, retain)UICollectionView *bottomView;
/// 滚动视图
@property (nonatomic, retain) UIScrollView *scrollView;

@end

@implementation GoodsMoreViewController

- (void)netWorking
{
    [_typeArray removeAllObjects];
    _typeArray = nil;
    self.typeArray = [[NSMutableArray alloc] init];
    NSString *string = @"http://hunliji.com/productCatefories.json";
    [NetHandler networkWithURL:string completion:^(id result) {
        NSArray *array =[result objectForKey:@"categories"];
        for (NSDictionary *dic in array) {
            TypeModel *model = [[TypeModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.typeArray addObject:model];
        }
        [self.bottomView reloadData];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 二级页面隐藏tabBar
    self.tabBarController.tabBar.hidden = YES;
    // 重写返回上级页面方法
    UIImage *backImage = [[UIImage imageNamed:@"返回.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem= [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(popBack:)];
    [self createSubViews];
    [self netWorking];
}
/// 返回上级页面
- (void)popBack:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

/// 创建视图->集合视图+滚动视图(进行控制)
- (void)createSubViews
{
    /// 集合视图
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width - 1, self.view.frame.size.height- 64 - 35);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.minimumLineSpacing = 1;
    
    self.bottomView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 26, self.view.frame.size.width, self.view.frame.size.height - 44) collectionViewLayout:flowLayout];
    
    _bottomView.backgroundColor = [WebColor white];
    _bottomView.pagingEnabled = YES;
    _bottomView.delegate = self;
    _bottomView.dataSource = self;
    [_bottomView registerClass:[GoodsMoreCollectionViewCell class] forCellWithReuseIdentifier:@"CK_hunsha"];
    [_bottomView registerClass:[GoodsMoreCollectionViewCell class] forCellWithReuseIdentifier:@"CK_lifu"];
    [_bottomView registerClass:[GoodsMoreCollectionViewCell class] forCellWithReuseIdentifier:@"CK_hunxie"];
    [_bottomView registerClass:[GoodsMoreCollectionViewCell class] forCellWithReuseIdentifier:@"CK_peishi"];
    [_bottomView registerClass:[GoodsMoreCollectionViewCell class] forCellWithReuseIdentifier:@"CK_shoushi"];
    [_bottomView registerClass:[GoodsMoreCollectionViewCell class] forCellWithReuseIdentifier:@"CK_hunqingyongpin"];
    [_bottomView registerClass:[GoodsMoreCollectionViewCell class] forCellWithReuseIdentifier:@"CK_jiajulipin"];
    [self.view addSubview:_bottomView];
    
    
    /// 滚动视图
    self.scrollView = [[UIScrollView alloc]init];
    [self.view addSubview:self.scrollView];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.alwaysBounceHorizontal = NO;
    self.scrollView.bounces = NO;
//    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.frame = CGRectMake(0, 0, WIDTH, 35);
    self.scrollView.backgroundColor = [WebColor white];
    self.scrollView.contentSize = CGSizeMake(WIDTH * 1.3, -20);
    
    
    
    CGFloat x = (WIDTH * 1.4) / 9;
    NSArray *array = [NSArray arrayWithObjects:@"婚纱", @"礼服", @"婚鞋", @"配饰", @"首饰", @"婚庆用品", @"家居礼品", nil];
    for (int i = 0; i < array.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        if (i == 0) {
            [button setBackgroundColor:[WebColor crimson]];
            [button setTitleColor:[WebColor white] forState:UIControlStateNormal];
        } else {
            [button setBackgroundColor:[WebColor white]];
            [button setTitleColor:[WebColor lightPink] forState:UIControlStateNormal];
        }
        button.tag = i + 23230;
        if (i < 5) {
            [button setFrame:CGRectMake(x * i + 5, 5, x - 5, 25)];
        } else if (i == 5){
            [button setFrame:CGRectMake(x * i + 5, 5, 2 * x - 25, 25)];
        } else {
            [button setFrame:CGRectMake(x * i - 15 + x, 5, 2 * x - 25, 25)];
        }
        [button setTitle:[array objectAtIndex:i] forState:UIControlStateNormal];
        button.layer.cornerRadius = 3;
        button.titleLabel.font = [UIFont boldSystemFontOfSize:17];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:button];
    }
}
- (void)buttonAction:(UIButton *)button
{
    // 偏移量
    [self.bottomView setContentOffset:CGPointMake(self.view.frame.size.width * (button.tag - 23230), 0) animated:YES];
    // 按钮的高亮状态
    for (int i = 0; i < 7; i ++) {
        UIButton *but = (UIButton *)[self.scrollView viewWithTag:23230 + i];
        [but setBackgroundColor:[WebColor white]];
        [but setTitleColor:[WebColor lightPink] forState:UIControlStateNormal];
    }
    [button setBackgroundColor:[WebColor crimson]];
    [button setTitleColor:[WebColor white] forState:UIControlStateNormal];
}
#pragma mark 集合视图协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.typeArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    switch (indexPath.item) {
        case 0:
        {
            GoodsMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_hunsha" forIndexPath:indexPath];
            cell.model = self.typeArray[indexPath.item];
            cell.block = ^(GoodsModel *model){
                ProGoodsViewController *goodsController = [[ProGoodsViewController alloc] init];
                goodsController.model = model;
                [self.navigationController pushViewController:goodsController animated:YES];
            };
            return cell;
        }
            break;
        case 1:
        {
            GoodsMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_lifu" forIndexPath:indexPath];
            cell.block = ^(GoodsModel *model){
                ProGoodsViewController *goodsController = [[ProGoodsViewController alloc] init];
                goodsController.model = model;
                [self.navigationController pushViewController:goodsController animated:YES];
            };
            cell.model = self.typeArray[indexPath.item];
            return cell;
        }
            break;
        case 2:
        {
            GoodsMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_hunxie" forIndexPath:indexPath];
            cell.model = self.typeArray[indexPath.item];
            cell.block = ^(GoodsModel *model){
                ProGoodsViewController *goodsController = [[ProGoodsViewController alloc] init];
                goodsController.model = model;
                [self.navigationController pushViewController:goodsController animated:YES];
            };
            return cell;
        }
            break;
        case 3:
        {
            GoodsMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_peishi" forIndexPath:indexPath];
            cell.model = self.typeArray[indexPath.item];
            cell.block = ^(GoodsModel *model){
                ProGoodsViewController *goodsController = [[ProGoodsViewController alloc] init];
                goodsController.model = model;
                [self.navigationController pushViewController:goodsController animated:YES];
            };
            return cell;
        }
            break;
        case 4:
        {
            GoodsMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_shoushi" forIndexPath:indexPath];
            cell.model = self.typeArray[indexPath.item];
            cell.block = ^(GoodsModel *model){
                ProGoodsViewController *goodsController = [[ProGoodsViewController alloc] init];
                goodsController.model = model;
                [self.navigationController pushViewController:goodsController animated:YES];
            };
            return cell;
        }
            break;
        case 5:
        {
            GoodsMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_hunqingyongpin" forIndexPath:indexPath];
            cell.model = self.typeArray[indexPath.item];
            cell.block = ^(GoodsModel *model){
                ProGoodsViewController *goodsController = [[ProGoodsViewController alloc] init];
                goodsController.model = model;
                [self.navigationController pushViewController:goodsController animated:YES];
            };
            return cell;
        }
            break;
        case 6:
        {
            GoodsMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_jiajulipin" forIndexPath:indexPath];
            cell.model = self.typeArray[indexPath.item];
            cell.block = ^(GoodsModel *model){
                ProGoodsViewController *goodsController = [[ProGoodsViewController alloc] init];
                goodsController.model = model;
                [self.navigationController pushViewController:goodsController animated:YES];
            };
            return cell;
        }
            break;
            
        default:{
            UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CK_zhanwei" forIndexPath:indexPath];
            return cell;
        }
            break;
    }
}
/// 监视偏移结束的协议
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView == _bottomView) {
        NSInteger tag = (_bottomView.contentOffset.x + 10)/ self.bottomView.frame.size.width;
        for (int i = 0; i < 7; i ++) {
            UIButton *but = (UIButton *)[self.scrollView viewWithTag:23230 + i];
            [but setBackgroundColor:[WebColor white]];
            [but setTitleColor:[WebColor lightPink] forState:UIControlStateNormal];
        }
        UIButton *button = (UIButton *)[self.scrollView viewWithTag:(tag + 23230)];
        [button setBackgroundColor:[WebColor crimson]];
        [button setTitleColor:[WebColor white] forState:UIControlStateNormal];
        if (_scrollView.contentOffset.x < self.view.center.x && tag > 2) {
            [_scrollView setContentOffset:CGPointMake(40 * tag - 115, 0) animated:YES];
        }
//        if (tag > 4 && tag < 7) {
//            [_scrollView setContentOffset:CGPointMake(40 * tag - 110, 0) animated:YES];
//            
//        }
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
